import time

from common.base_z import Base, By, get_driver


class ShopCar(Base):
    def username_input(self, username):
        self.send_keys((By.NAME, 'username'), username)

    def password_input(self, password):
        self.send_keys((By.NAME, 'password'), password)

    def remember_checkbox(self):
        self.click((By.ID, 'remember'))

    def submit_button(self):
        self.click((By.NAME, 'submit'))

    def clothes_click(self):  # 定位点击服装按钮
        self.click((By.XPATH, '//*[@id="mainNav"]/div/ul/li[2]/a'))

    def T_click(self):  # 定位点击T恤
        self.click((By.XPATH, '/html/body/div[4]/div[2]/div/div[1]/form/div/div/div[3]/div/div[3]/a'))

    def buy_click(self):  # 点击立即购买加入购物车
        self.click((By.XPATH, '//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img'))

    def clear_click(self):  # 清空购物车
        self.click((By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]'))

    def check_shopcar(self):  # 点击购物车确认清空
        self.click((By.XPATH, '//*[@id="ECS_CARTINFO"]/a'))


if __name__ == '__main__':
    shop = ShopCar(get_driver())
    shop.get('http://localhost:8080/ecshop/user.php')
    # 测试方法
    shop.username_input('root')
    shop.password_input('123456')
    shop.remember_checkbox()
    shop.submit_button()
    time.sleep(2)

    shop.clothes_click()
    shop.T_click()
    shop.buy_click()
    shop.clear_click()
    shop.check_shopcar()
